package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.common.utils.ToolsUtils;
import zhihuilan.dto.MyCircleDto;
import zhihuilan.dto.MyFactoryFavDto;
import zhihuilan.dto.MyStaffShopGoodsDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyFactoryFav;
import zhihuilan.entity.MyShop;
import zhihuilan.entity.MyStaff;
import zhihuilan.mapper.MyFactoryFavMapper;
import zhihuilan.params.query.MyFactoryFavQueryParam;
import zhihuilan.service.MyFactoryFavService;
import zhihuilan.service.MyShopService;
import zhihuilan.service.MyStaffService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyFactoryFavServiceImpl extends ServiceImpl<MyFactoryFavMapper, MyFactoryFav> implements MyFactoryFavService {

    @Resource
    private RedisCache redisCache;

    @Resource
    private MyStaffService myStaffService;
    @Resource
    private MyShopService myShopService;

    @Resource
    private MyFactoryFavMapper myFactoryFavMapper;

    @Override
    public WxMiniPageDto<MyFactoryFavDto> selectPageList(MyFactoryFavQueryParam queryParam) {
        QueryWrapper<MyFactoryFav> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staffid", queryParam.getStaffId());
            }
            if (!ObjectUtils.isEmpty(queryParam.getFavStaffid())) {
                queryWrapper.eq("fav_staffid", queryParam.getFavStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getType())) {
                queryWrapper.eq("type", queryParam.getType());
            }
        }
        queryWrapper.orderByDesc("addtime");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyFactoryFav> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyFactoryFavDto> result = new WxMiniPageDto(page.getCurrent(), page.getSize());
        BeanUtils.copyProperties(page, result);
        List<MyFactoryFavDto> convertList = page.getRecords().stream().map(po -> {
            MyFactoryFavDto dto = new MyFactoryFavDto();
            BeanUtils.copyProperties(po, dto);
            Map<String, Object> map = new HashMap<>();

            if (!ObjectUtils.isEmpty(dto.getFactoryid()) && dto.getFactoryid() > 0) {
//
//                map.put("logo", "");
//                map.put("title", "市场用户[" + dto.getFactoryid() + "]已停用");
//                map.put("staffid", dto.getFavStaffid());
            } else if (!ObjectUtils.isEmpty(dto.getShopid()) && dto.getShopid() > 0) {
                MyShop shop = this.myShopService.selectShopByById(dto.getShopid());
                try {
                    map = ToolsUtils.toMap(shop);
                } catch (IllegalAccessException e) {

                }
            } else {
                MyStaff staff = this.myStaffService.selectMyStaffById(dto.getFavStaffid());
                try {
                    map = ToolsUtils.toMap(staff);
                    map.put("logo", !ObjectUtils.isEmpty(staff.getLogo()) ? staff.getLogo() : staff.getHeadimgurl());
                    map.put("title", !ObjectUtils.isEmpty(staff.getTitle()) ? staff.getTitle() : staff.getUserreal());
                    map.put("staffid", dto.getFavStaffid());
                } catch (IllegalAccessException e) {

                }
            }
            dto.setInfo(map);
            return dto;
        }).collect(Collectors.toList());
        result.setData(convertList);
        return result;
    }

    @Override
    public Long selectCount(MyFactoryFavQueryParam queryParam) {
        QueryWrapper<MyFactoryFav> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staffid", queryParam.getStaffId());
            }
            if (!ObjectUtils.isEmpty(queryParam.getFavStaffid())) {
                queryWrapper.eq("fav_staffid", queryParam.getFavStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getType())) {
                queryWrapper.eq("type", queryParam.getType());
            }
        }
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public MyFactoryFav addEntity(MyFactoryFav entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        entity.setAddtime(new Date());
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public Boolean deleteById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean deleteByStaffId(Integer staffId, Integer favStaffId) {
        QueryWrapper<MyFactoryFav> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffid", staffId);
        queryWrapper.eq("fav_staffid", favStaffId);
        queryWrapper.eq("type", 0);
        return this.baseMapper.delete(queryWrapper) > 0;
    }

    @Override
    public Boolean deleteByShopId(Integer shopId) {
        QueryWrapper<MyFactoryFav> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shopid", shopId);
        return this.baseMapper.delete(queryWrapper) > 0;

    }

    @Override
    public WxMiniPageDto<MyCircleDto> selectCirclePageList(MyFactoryFavQueryParam queryParam, MyStaff currentStaff) {
        if (ObjectUtils.isEmpty(queryParam.getStaffId())) {
            return new WxMiniPageDto<MyCircleDto>();
        }
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        if (pageNum > 0) {
            pageNum = pageSize * (pageNum - 1);
        }
        String regionCode = queryParam.getDistrictid();
        Integer regionLevel = 0;
        if (!ObjectUtils.isEmpty(queryParam.getDistrictid())) {
            if (queryParam.getDistrictid().length() == 6) {
                regionLevel=3;
            } else if (queryParam.getDistrictid().length() == 4) {
                regionLevel=2;
            } else if (queryParam.getDistrictid().length() == 2) {
                regionLevel=1;
            } else if (!ObjectUtils.isEmpty(queryParam.getProvinceid())) {
                regionCode=queryParam.getProvinceid();
                regionLevel=1;
            }
        }

        Boolean isVideo = false;
        if (queryParam.getUtype().equals(1)) {
            isVideo = true;
        }
        List<MyCircleDto> lsit = this.baseMapper.selectPageListToCircle(pageNum, pageSize, queryParam.getStaffId(), regionCode, regionLevel, isVideo);
        Long count = this.baseMapper.selectCountToCircle(queryParam.getStaffId(), regionCode, regionLevel, isVideo);
        WxMiniPageDto<MyCircleDto> result = new WxMiniPageDto(pageNum, pageSize);
        List<MyCircleDto> convertList = lsit.stream().map(po -> {
            po.setVideo(StringEscapeUtils.unescapeHtml4(po.getVideo()));
            if (!ObjectUtils.isEmpty(currentStaff) && !ObjectUtils.isEmpty(currentStaff.getId())) {
                QueryWrapper<MyFactoryFav> queryWrapperFav = new QueryWrapper<>();
                queryWrapperFav.eq("staffid", currentStaff.getId());
                queryWrapperFav.eq("fav_staffid", po.getStaffId());
                Long favCount = this.myFactoryFavMapper.selectCount(queryWrapperFav);
                if (ObjectUtils.isEmpty(favCount) || favCount == 0L) {
                    po.setIsfav(0);
                } else {
                    po.setIsfav(1);
                }
            } else {
                po.setIsfav(0);
            }
            return po;
        }).collect(Collectors.toList());
        result.setData(convertList);
        result.setTotal(count);
        return result;
    }
}
